package yz;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * [876. 链表的中间结点](https://leetcode-cn.com/problems/middle-of-the-linked-list/)
 *
 * @author Yz
 * @since 2022-09-26 20:18
 */
public class LeetCode876_MiddleOfTheLinkedList {
    public ListNode middleNode(ListNode head) {
        AtomicInteger atomicInteger = new AtomicInteger(1);
        ListNode next = head.next;
        while (next != null) {
            atomicInteger.getAndIncrement();
            next = next.next;
        }
        int length = atomicInteger.get();
        int num = length / 2 + 1;
        for (int i = 0; i < num - 1; i++) {
            head = head.next;
        }
        return head;
    }
    
    static class ListNode {
        int var;
        ListNode next;
        
        public ListNode() {
        }
        
        public ListNode(int var) {
            this.var = var;
        }
        
        public ListNode(int var, ListNode next) {
            this.var = var;
            this.next = next;
        }
    }
}
